In Object Oriented programming, a subclass inherits the data structure (also known as the attributes) and behaviors (also known as the methods) from the subclass' superclass(es) (also known as parent classes). Frequently the term “properties” is used to collectively refer to attributes and methods. In the case of methods, the subclasses may either: (1) use the methods of the superclass as these methods are implemented in the superclass; or (2) the subclasses may override the methods of the superclass. In the case of attributes, the subclasses inherit the attributes of the subclasses' superclasses but there is no overriding of attributes. FIG. 1a is a block diagram that depicts a situation where (1) a method (e.g. “publish”) is inherited and overridden; and (2) an attribute (e.g., string x) is inherited.
In discussing the case of methods with respect to FIG. 1a, assume that superclass 100 has a publish method. The implementation of the publish method in superclass 100 may involve emailing to a news group (hereinafter referred to as “publish by email to news group”). Subclasses 110 and 113 both override the publish method in superclass 100. For example, subclass 110 may implement the publish method by broadcasting (hereinafter referred to as “publish by broadcast”) and subclass 113 may implement the publish method by opening it for review (hereinafter referred to as “publish by open for review”). Subclass 116 does not override the implementation of the publish method of superclass 100.
In overriding a method, the method in the subclass continues to have the same name as the method of the superclass. Only the implementation of the method in the subclass is different from the implementation in the superclass.
A class can be compared to a “cookie cutter”. An instance (also known as an object) of a class can be thought of as dough that has been “stamped out” by this “cookie cutter” or class. Therefore, each instance of a class has all of the properties defined by the class that was used to “stamp out” that particular instance/object.
Each object has a “line of inheritance”. In other words, object 111's line of inheritance starts with subclass 110 and is followed by superclass 100. If a particular method such as “publish” is requested of object 111, subclass 110 is first checked to see if the “publish” method is implemented in subclass 110. If the “publish” method type is implemented in subclass 110, that “publish” method implementation is used. Otherwise, the superclass 100 is checked to see if the “publish” method is implemented in superclass 100. Searching for the requested method “publish” continues up the “line of inheritance” until the requested method is found.
Furthermore, in Object Oriented programming, all instances of a particular class share common implementations of the methods of that class. In other words, objects 111 and 112 will have the same publish method implementation (“publish by broadcast”), which was obtained from subclass 110. Objects 114 and 115 will have the same publish method implementation (“publish by open for review”), which was obtained from subclass 113. Objects 117 and 118 will have the same publish method implementation, which was obtained from superclass 100 (“publish by email to news group”).
In Object Oriented programming it is not possible for object 111 to have a different implementation of the publish method than object 112 because both object 111 and object 112 are instances of the same class, subclass 110. In other words, it is not possible with classic Object Oriented programming for object 111 to have a “publish by broadcast” implementation while object 112 has a “publish by open for review” implementation. Therefore, classic inheritance cannot be used to provide different implementations for a particular method for objects in the same class.
Subclasses cannot override attributes of their superclasses, but the subclasses may introduce additional attributes that do not belong to their superclasses. In discussing the case of attributes with respect to FIG. 1a, assume that superclass 100 has a string x, subclass 110 has a string y, subclass 113 has a string z, and subclass 116 does not introduce another string. In this situation, superclass 100 would only have string x. Subclass 110 would inherit string x from superclass 100 and in addition would introduce string y. Therefore, objects 111 and 112 would have string x and string y as attributes. Subclass 113 would inherit string x from superclass 100 and in addition would introduce string z. Therefore, objects 114 and 115 would have string x and string z. Subclass 116 would only inherit string x from superclass 100. Therefore objects 117 and 118 would only have string x.
In Object Oriented programming it is not possible for object 111 to have a different set of attributes than object 112 because both object 111 and object 112 are instances of the same class, subclass 110. In other words, it is not possible with classic Object Oriented programming for object 111 to have a string y but not string z, while object 112 has a string z but not a string y. Therefore, classic inheritance cannot be used to provide different attributes for instances of the same class.